package com.java.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.java.dao.HomeTemplateDao;
import com.java.dao.HomeTemplateDetailDao;
import com.java.dao.ProductDao;
import com.java.vo.ShopHomeTemplate;
import com.java.vo.ShopHomeTemplateDetail;

/**
 * Created by v.sc.d on 2017/4/12.
 */
@Service
@Transactional
public class ShopHomeTemplateService {
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private HomeTemplateDao homeTemplateDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private HomeTemplateDetailDao homeTemplateDetailDao;

	public ShopHomeTemplate getShopHomeTemplateById(int id) {
		// 获取可显示的所有专区
		ShopHomeTemplate shopHomeTemplate = homeTemplateDao.getShopHomeTemplateById(id);
		if (shopHomeTemplate == null) {
			return null;
		}

		// 根据专区id获取专区内容
		List<ShopHomeTemplateDetail> homeTemplateDetails = homeTemplateDetailDao
				.getShopHomeTemplateDetailByTemplateIdAndGroup(shopHomeTemplate.getId());
		if (homeTemplateDetails == null) {
			return shopHomeTemplate;
		}

		for (int j = 0; j < homeTemplateDetails.size(); j++) {

			ShopHomeTemplateDetail shopHomeTemplateDetail = homeTemplateDetails.get(j);
			// 遍历专区内容，将三合一的专列进行内容填充
			if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
				List<ShopHomeTemplateDetail> subHomeTemplateDetails = homeTemplateDetailDao
						.getShopHomeTemplateDetailByTemplateIdAndGroupId(shopHomeTemplate.getId(),
								shopHomeTemplateDetail.getDetailGroupId());
				if (subHomeTemplateDetails != null) {
					for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
						ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
						homeTemplateDetail
								.setShopProduct(productDao.getShopProductById(homeTemplateDetail.getProductId()));
						subHomeTemplateDetails.set(x, homeTemplateDetail);
					}
					shopHomeTemplateDetail.setShopHomeTemplateDetails(subHomeTemplateDetails);
					homeTemplateDetails.set(j, shopHomeTemplateDetail);
				}
			} else {
				shopHomeTemplateDetail
						.setShopProduct(productDao.getShopProductById(shopHomeTemplateDetail.getProductId()));
			}
		}
		shopHomeTemplate.setHomeTemplateDetails(homeTemplateDetails);
		return shopHomeTemplate;
	}

	@Transactional
	public boolean addShopHomeTemplate(ShopHomeTemplate shopHomeTemplate) {
		// 获取专区下所有横幅
		List<ShopHomeTemplateDetail> homeTemplateDetails = shopHomeTemplate.getHomeTemplateDetails();

		// 如果横幅列表为空则返回
		if (homeTemplateDetails == null) {
			return false;
		}

		//先插入专区表
		int HomeTemplateId = homeTemplateDao.insertShopHomeTemplate(shopHomeTemplate);
		if (HomeTemplateId < 0) {
			return false;
		}

		//遍历横幅的内容
		for (int j = 0; j < homeTemplateDetails.size(); j++) {

			ShopHomeTemplateDetail shopHomeTemplateDetail = homeTemplateDetails.get(j);
			// 遍历横幅内容，将三合一的专列区分插入
			if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
				
				List<ShopHomeTemplateDetail> subHomeTemplateDetails = shopHomeTemplateDetail.getShopHomeTemplateDetails();
				
				if (subHomeTemplateDetails != null) {
					for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
						ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
						homeTemplateDetail.setHomeTemplateId(HomeTemplateId);
						homeTemplateDetailDao.insertShopHomeTemplateDetail(homeTemplateDetail);
					}
				}
			} else {
				shopHomeTemplateDetail.setHomeTemplateId(HomeTemplateId);
				homeTemplateDetailDao.insertShopHomeTemplateDetail(shopHomeTemplateDetail);
			}
		}
		return true;
	}
	
	@Transactional
	public boolean updateShopHomeTemplate(ShopHomeTemplate shopHomeTemplate) {
		// 获取专区下所有横幅
		List<ShopHomeTemplateDetail> homeTemplateDetails = shopHomeTemplate.getHomeTemplateDetails();

		// 如果横幅列表为空则返回
		if (homeTemplateDetails == null) {
			return false;
		}

		//先插入专区表
		int HomeTemplateId = homeTemplateDao.UpdateShopHomeTemplate(shopHomeTemplate);
		if (HomeTemplateId < 0) {
			return false;
		}

		//遍历横幅的内容
		for (int j = 0; j < homeTemplateDetails.size(); j++) {

			ShopHomeTemplateDetail shopHomeTemplateDetail = homeTemplateDetails.get(j);
			// 遍历横幅内容，将三合一的专列区分插入
			if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
				
				List<ShopHomeTemplateDetail> subHomeTemplateDetails = shopHomeTemplateDetail.getShopHomeTemplateDetails();
				
				if (subHomeTemplateDetails != null) {
					for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
						ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
						homeTemplateDetail.setHomeTemplateId(HomeTemplateId);
						homeTemplateDetailDao.UpdateShopHomeTemplateDetail(homeTemplateDetail);
					}
				}
			} else {
				shopHomeTemplateDetail.setHomeTemplateId(HomeTemplateId);
				homeTemplateDetailDao.UpdateShopHomeTemplateDetail(shopHomeTemplateDetail);
			}
		}
		return true;
	}
	
	@Transactional
	public boolean delShopHomeTemplateDetail(ShopHomeTemplate spdb,ShopHomeTemplate shopHomeTemplate) {
		
		List<ShopHomeTemplateDetail> homeTemplateDetails = shopHomeTemplate.getHomeTemplateDetails();

		if (homeTemplateDetails == null || spdb == null) {
			return false;
		}

		ArrayList<Integer> detailIds = new ArrayList<>(); 
		//遍历横幅的内容
		for (int j = 0; j < homeTemplateDetails.size(); j++) {

			ShopHomeTemplateDetail shopHomeTemplateDetail = homeTemplateDetails.get(j);
			// 遍历横幅内容，将三合一的专列区分插入
			if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
				
				List<ShopHomeTemplateDetail> subHomeTemplateDetails = shopHomeTemplateDetail.getShopHomeTemplateDetails();
				
				if (subHomeTemplateDetails != null) {
					for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
						ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
						if(homeTemplateDetail.getId() > 0){
							detailIds.add(homeTemplateDetail.getId());
						}
					}
				}
			} else {
				if(shopHomeTemplateDetail.getId() > 0){
					detailIds.add(shopHomeTemplateDetail.getId());
				}
			}
		}
		
		logger.info("仍存在的id们:"+detailIds.toString());
		
		for (int j = 0; j < spdb.getHomeTemplateDetails().size(); j++) {

			ShopHomeTemplateDetail shopHomeTemplateDetail = spdb.getHomeTemplateDetails().get(j);
			// 遍历横幅内容，将三合一的专列区分插入
			if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
				
				List<ShopHomeTemplateDetail> subHomeTemplateDetails = shopHomeTemplateDetail.getShopHomeTemplateDetails();
				
				if (subHomeTemplateDetails != null) {
					for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
						ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
						if(!detailIds.contains(homeTemplateDetail.getId())){
							logger.info("删除的:"+homeTemplateDetail.getId());
							homeTemplateDetailDao.delShopHomeTemplateDetailById(homeTemplateDetail.getId());
						}
					}
				}
			} else {
				if(!detailIds.contains(shopHomeTemplateDetail.getId())){
					logger.info("删除的:"+shopHomeTemplateDetail.getId());
					homeTemplateDetailDao.delShopHomeTemplateDetailById(shopHomeTemplateDetail.getId());
				}
			}
		}
		return true;
	}
	
}